﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;

namespace CS_BEGINNER_TO_PROFICIENT.STD.ReflectionSTD
{
    [AttributeUsage(AttributeTargets.Method)]
    public class TestAttribute : Attribute { }

    public class MyTest
    {
        [Test]
        public void TestMethod1()
        {
            Console.WriteLine("TestMethod1 executed.");
        }  
        
        [Test]
        public void TestMethod2()
        {
            Console.WriteLine("TestMethod2 executed.");
        }
    }

    public class ReflectionSTDP3
    {
        public static void Execute()
        {
            var testClassType = typeof(MyTest);
            var methods = testClassType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                                       .Where(m => m.GetCustomAttributes<TestAttribute>().Any());

            var testClassInstance = Activator.CreateInstance(testClassType);

            foreach (var method in methods)
            {
                method.Invoke(testClassInstance, null);
                Console.WriteLine($"Executed {method.Name}");
            }
        }
    }
}
